﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Windows;
using UpdaterApp;
using UpdaterComponents.Common;

namespace UpdaterAppWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private readonly List<Thread> threadList;
        private bool bIsUpgradeCompleted = false;
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
            this.Closed += new EventHandler(MainWindow_Closed);
            threadList = new List<Thread>();
        }

        void MainWindow_Closed(object sender, EventArgs e)
        {

            foreach (Thread th in threadList)
            {
                if (th.Name == "UpdaterThread")
                    th.Abort();
            }

            if(!bIsUpgradeCompleted)          
                MessageBox.Show("Upgrade Failure : Refer \"UpgradeLog.txt\" for further details", "FtpUploadClient Upgrade Failure",
    MessageBoxButton.OK, MessageBoxImage.Error);
           
           
           
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            



                if (File.Exists("UpgradeLog.txt"))
                    File.Delete("UpgradeLog.txt");

                if (File.Exists("Updates.xml"))
                {

                    Thread th = new Thread(new ThreadStart(StartIt));
                    th.Name = "UpdaterThread";
                    threadList.Add(th);

                    th.Start();
                }
                else
                {
                    MessageBox.Show("Upgrade Failure : \"Updates.xml\" is not available", "FtpUploadClient Upgrade Failure", MessageBoxButton.OK, MessageBoxImage.Error);
                    this.Close();
                }
                    
        }
        
        public void StartIt()
        {

            QueProcessor que = QueProcessor.GetInstance();
            que.WorkProgress += new QueProcessor.WorkProgresshandler(que_WorkProgress);
            que.OnUpgradeCompleted += new QueProcessor.UpgradeCompletedHandler(que_OnUpgradeCompleted);
            que.IndicateTask += new QueProcessor.IndicateTaskhandler(que_IndicateTask);
            que.ProcessQue();
            que.WorkProgress -= new QueProcessor.WorkProgresshandler(que_WorkProgress);
            que.OnUpgradeCompleted -= new QueProcessor.UpgradeCompletedHandler(que_OnUpgradeCompleted);

            Dispatcher.Invoke((Action)(delegate { this.Close(); }));
        }

        void que_IndicateTask(string msg)
        {
            Dispatcher.Invoke((Action)(delegate { lblUpdating.Content = msg+"..."; }));
          
        }

        void que_OnUpgradeCompleted(bool bIsThereanyError)
        {

            bIsUpgradeCompleted = bIsThereanyError;

            //If there are no errors then
            //Clean the temp data
            //and close the updater and start the updated app
            if (!bIsThereanyError)
            {
                CommonMethods.CleanUpIfAnyUndeletedData();
                CommonMethods.KillCurrentProcessAndRestartApp();     

            }         
           
        }

        void que_WorkProgress(float fVal)
        {
            Dispatcher.Invoke((Action)(delegate { pbUpdate.Value = fVal;  }));
        }

        
    }
}
